﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class AddStudent_form : Form
    {
        public AddStudent_form()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = textBox1.Text.Trim();
            string name = textBox2.Text.Trim();
            string grade = textBox3.Text.Trim();
            string major = textBox4.Text.Trim();

            //表单验证
            if(id == "")
            {
                MessageBox.Show("请输入学号！");
                textBox1.Focus();
                return;
            }
            if(id.Length != 11)
            {
                MessageBox.Show("请输入正确的学号！");
                textBox1.Focus();
                return;
            }
            //验证是否已注册
            if(DBhelper.checkStu(id))
            {
                MessageBox.Show("该学生已经注册了！");
                textBox1.Focus();
                return;
            }
            if (name == "")
            {
                MessageBox.Show("请输入姓名！");
                textBox1.Focus();
                return;
            }
            if (grade == "")
            {
                MessageBox.Show("请输入年级！");
                textBox1.Focus();
                return;
            }
            if (major == "")
            {
                MessageBox.Show("请输入专业！");
                textBox1.Focus();
                return;
            }

            DialogResult dr = MessageBox.Show($"学号：{id}\r\n姓名：{name}\r\n年级：{grade}\r\n专业：{major}\r\n确定要注册吗？", "提示", MessageBoxButtons.YesNo);
            if(dr == DialogResult.Yes)
            {
                string sql = $"insert into StuInfo values('{id}','123456','{name}','{grade}','{major}',default)";
                if(DBhelper.NonQuery(sql))
                {
                    MessageBox.Show("注册成功！");
                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
                textBox1.Focus();
            }
        }
    }
}
